package com.jingdianjichi.subject.domain.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 *
 * 线程池的config管理
 *
 * @author Richard
 * date 2024/3/10 22:54
 */
@Configuration
public class ThreadPoolConfig {

    /*
    ThreadPoolExecutor.CallerRunsPolicy() 是 Java 线程池中的一种拒绝策略，它的作用
    是当线程池无法处理新提交的任务时，由提交任务的线程来执行该任务
     */
    //调用自定义线程工厂调用
    @Bean(name = "labelThreadPool")
    public ThreadPoolExecutor getLabelThreadPool(){
        return new ThreadPoolExecutor(20,100,5,
                TimeUnit.SECONDS,new LinkedBlockingDeque<>(40),
                new CustomNameThreadFactory("label"),
                new ThreadPoolExecutor.CallerRunsPolicy());
    }
//    @Bean(name = "labelThreadPool")
//    public ThreadPoolExecutor getLabelThreadPool(){
//        return new ThreadPoolExecutor(20,100,5,
//                TimeUnit.SECONDS,new LinkedBlockingDeque<>(40),
//                Executors.defaultThreadFactory(),
//                new ThreadPoolExecutor.CallerRunsPolicy());
//    }
}
